/*
** Copyright (C) 2010 Calin Radoni
**
** This file is part of CHScanner.
**
** CHScanner is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** CHScanner is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with CHScanner. If not, see <http://www.gnu.org/licenses/>.
*/

#include "StdAfx.h"
#include "MTLVRecord.h"

//---------------------------------------------------------------------------

MTLVRecord::MTLVRecord(void)
{
	xType   = 0x0000;
	xLength = 0x0000;
	xValue  = NULL;
}

MTLVRecord::~MTLVRecord(void)
{
	CleanUp();
}

//---------------------------------------------------------------------------

void MTLVRecord::CleanUp(void)
{
	if(xValue!=NULL){
		delete[] xValue;
		xValue = NULL;
	}
}

//---------------------------------------------------------------------------

void MTLVRecord::Initialize(void)
{
	CleanUp();
	xType   = 0x0000;
	xLength = 0x0000;
}

//---------------------------------------------------------------------------

void MTLVRecord::LoadFromPointer(MTLVRecord* recordIn)
{
	int reqSize;

	Initialize();
	if(recordIn==NULL) return;

	reqSize = recordIn->xLength;
	if(reqSize<4) return;	// invalid length

	xType   = recordIn->xType;
	xLength = recordIn->xLength;

	reqSize -= 4;
	if(reqSize==0) return;

	try{
		xValue = new BYTE[reqSize];
	}
	catch(...){
		xValue = NULL;
	}
	if(xValue==NULL){
		Initialize();
		return;
	}

	for(int i=0; i<reqSize; i++)
		xValue[i] = recordIn->xValue[i];
}

//---------------------------------------------------------------------------

bool MTLVRecord::AddValueFromBytePtr(BYTE* dataIn, int sizeIn)
{
	if(sizeIn<0) return false;
	
	CleanUp();

	if(dataIn==NULL) return true;
	if(sizeIn==0) return true;

	try{
		xValue = new BYTE[sizeIn];
	}
	catch(...){
		xValue = NULL;
	}
	if(xValue==NULL) return false;

	memcpy(xValue, dataIn, sizeIn);

	return true;
}

//---------------------------------------------------------------------------

bool MTLVRecord::IsEmpty(void)
{
	return xLength==0 ? true : false;
}

bool MTLVRecord::IsEqualWith(MTLVRecord* recordIn)
{
	bool theSame;
	int i, cnt;

	if(recordIn==NULL) return false;

	if(xType   != recordIn->xType)     return false;
	if(xLength != recordIn->xLength)   return false;

	if(xLength<4) return false;	// these objects have an invalid length
	cnt = xLength - 4;
	if(cnt==0) return true;		// no data, compare no more

	theSame = true;
	for(i=0; i<cnt && theSame; i++)
		if(xValue[i]!=recordIn->xValue[i])
			theSame = false;

	return theSame;
}

//---------------------------------------------------------------------------
